# _*_ coding:utf8 _*_
from django.http import JsonResponse
from rest_framework.exceptions import APIException
from rest_framework.views import exception_handler

ErrorBase = 600
ErrorDoesNotExist = ErrorBase + 1

# Login
ErrorLoginBase = ErrorBase + 10
ErrorLoginPasswordIncorrect = ErrorLoginBase + 1
ErrorLoginInvalidVerifyCode = ErrorLoginBase + 2
ErrorLoginVerifyFail = ErrorLoginBase + 3


class RoingException(APIException):
    def __init__(self, message, code, rcode=0):
        super(RoingException, self).__init__(detail=message, code=code)
        self.status_code = code
        self.roing_code = rcode

        if rcode:
            self.status_code = 500


def roing_exception_handler(exc, context):
    response = exception_handler(exc, context)
    if response is not None:
        response.data['code'] = response.status_code

    if response.status_code == 500:
        response = JsonResponse({'code': exc.roing_code, 'message': exc.detail})
    return response